package com.fh.shop.api.order.controller;

import com.fh.shop.api.annotation.Idempotence;
import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConstant;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.order.biz.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/orders")
@Api(tags = "订单接口控制类")
public class OrderController {
    @Autowired
    private HttpServletRequest request;
    @Resource(name = "orderService")
    private IOrderService orderService;

    @PostMapping("/submitOrder")
    @NeedLogin
    @Idempotence
    @ApiOperation("提交订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recipientId", value = "收件人id", example = "0"),
            @ApiImplicitParam(name = "payType", value = "支付类型", example = "0"),
            @ApiImplicitParam(name = "x-auth", value = "请求头", dataType = "java.lang.String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "x-idempotence", value = "请求头", dataType = "java.lang.String", required = true, paramType = "header")
    })
    public ServerResponse submitOrder(Long recipientId, Integer payType) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return orderService.submitOrder(recipientId, payType, memberId);
    }

    @GetMapping("/findOrderList")
    @NeedLogin
    @ApiOperation("查询订单列表")
    @ApiImplicitParam(name = "x-auth", value = "请求头", dataType = "java.lang.String", required = true, paramType = "header")
    public ServerResponse findOrderList() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConstant.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return orderService.findOrderList(memberId);
    }
}
